package csc.fresher_group2.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.org.apache.bcel.internal.generic.ILOAD;

import csc.fresher_group2.dao.IFileDAO;
import csc.fresher_group2.dao.IFileDAOImpl;
import csc.fresher_group2.dao.ILog;
import csc.fresher_group2.dao.ILogImpl;
import csc.fresher_group2.model.Account;
import csc.fresher_group2.model.UserLog;

/**
 * Servlet implementation class HistoryServlet
 */
public class HistoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HistoryServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			doActionFiles(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @author Phuc
	 * @param request
	 * @param response
	 */
	protected void doActionFiles(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Account user = (Account) session.getAttribute("account");
		String page = "history.jsp";
		//IFileDAO iFileDao = new IFileDAOImpl();
		//iFileDao.getFiles()
		//iFileDao.newFolder(getServletContext().getRealPath("\\users\\"), false);
		if (user == null) {
			response.sendRedirect("login.jsp");
		} else {
			String userName = user.getUserName();
			ILog iLog = new ILogImpl();
			List<UserLog> logs = iLog.getAllLogs(userName);
			request.setAttribute("lInfos", logs);
		request.getRequestDispatcher(page).forward(request, response);
		}
	}
}
